/*
 * @Author: liuxinyea
 * @Date: 2024-06-12 09:43:42
 * @LastEditors: liuxinye
 * @LastEditTime: 2024-08-20 13:44:48
 * @Description: 可移动角色控制器
 */
import { _decorator, CircleCollider2D, Component, ERigidBody2DType, EventKeyboard, Input, input, KeyCode, Node, RigidBody2D, UITransform } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('PlayerCtrl')
export class PlayerCtrl extends Component {
    mRigidBody: RigidBody2D;
    mCollider: CircleCollider2D;
    start() {
        this.initEventListener();
        this.initPyhysics()
    }
    // 添加碰撞体和刚体组件
    initPyhysics() {
        // 物理组件
       this.mRigidBody = this.node.addComponent(RigidBody2D);
       this.mRigidBody.type = ERigidBody2DType.Dynamic;
       this.mRigidBody.gravityScale = 1;
       // 碰撞体组件添加
       this.mCollider = this.node.addComponent(CircleCollider2D);
       const transform = this.node.getComponent(UITransform);
       this.mCollider.radius = transform.width / 2;
    }
    //  初始化事件监听
    initEventListener() {
       input.on(Input.EventType.KEY_DOWN, this.onKeyDown , this);
    }
    onKeyDown(event: EventKeyboard){
        console.log("key down");
        switch (event.keyCode) {
            case KeyCode.KEY_A:
                // this.node.setPosition
                break;
            case KeyCode.KEY_D:
                break;
            case KeyCode.SPACE:
                break;
        }
    } 
    update(deltaTime: number) {
        
    }
    lateUpdate(deltaTime: number) {
    }
}


